<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$setting=$this->plugin_setting('setting');
$tabs=array(
	array('op'=>'list','title'=>'分销商等级管理'),
	array('op'=>'edit','title'=>'添加/编辑等级'),
);
set_html('tabs',$tabs);
$page_setting=$this->plugin_setting('page_setting');
$return['shoplevels']=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
//添加默认等级
$return['shoplevels'][0]=array(
		'id'=>'0',
		'title'=>'默认会员',
		'level'=>0,
		'discount'=>'0',
		'ordermoney'=>'0.00',
		'ordercount'=>'0',
		'status'=>'1',
);
if($_RQ['op']=='list'){	
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='level asc';
	$total='';
	$return['list']=$this->getall('level',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array();
		$item['conditions']=$page_setting['demolevel'];
	}
	//添加默认等级
	$default_level=array(
		'id'=>'0',
		'title'=>$page_setting['demolevel'],
		'level'=>0,
		'shoplevel_id'=>0,
		'conditions'=>$page_setting['demolevel'],
		'commissions'=>$this->plugin_setting('default_level')?:array('commission1'=>'0.00'),
	);
	array_unshift($return['list'],$default_level);
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'默认等级重命名：移步--》样式设置--》默认等级','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'shoplevel_id','title'=>'绑定商城会员等级','type'=>'select','options'=>'shoplevels'),
		),'action'=>array(
			array('op'=>'look_condition','title'=>'升级条件','type'=>'modal','show'=>array('level >'=>0)),
			array('op'=>'edit','title'=>'编辑','type'=>'href','show'=>array('level >'=>0)),
			array('op'=>'set_default','title'=>'设置默认佣金','type'=>'href','show'=>array('level'=>0)),
		)),
	);
	if($setting['get_buyordernum']==1){
		$bilititle='级佣金额度（元/笔）';
		$unit='元';
	}else{
		$bilititle='级佣金比例（%）';
		$unit='%';
	}
	for($i=1;$i<=$setting['level'];$i++){
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.commission'.$i,'title'=>$i.$bilititle);
	}
	if($setting['jicha_open']==1){
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.ji_rate','title'=>'极差分红');
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.ping_rate','title'=>'平级分红');
	}
	if($setting['jicha_chao']==1){
		$htmls['list']['list'][]=array('type'=>'string','name'=>'commissions.ji_chao','title'=>'超级奖');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look_condition'){
	$item=$this->get('level',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('conditions'));
	if(!$item){
		exi('指定等级不存在！','error');
	}
	
	$item['conditions']=json_decode($item['conditions'],true);
	$return['item']=$item;
	if(!$item['conditions']){
		exi('该等级尚未设置升级条件！','error');
	}
	$htmls=array(
		'type'=>'look',
		'look'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		$htmls['look'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('level',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['conditions']=$item['conditions']?json_decode($item['conditions'],true):array('order_price'=>'0');
		$item['commissions']=$item['commissions']?json_decode($item['commissions'],true):array('commission1'=>'0.00');
	}else{
		$item=array(
			'title'=>'',
			'conditions'=>array('order_price'=>'0'),
			'commissions'=>array('commission1'=>'0.00'),
		);
	}
	$item['goods_ids']=$item['goods_ids']?json_decode($item['goods_ids'],true):array();
	$return['yidongtype']=array('不开启','扫团长二维码','任何分享');
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'等级设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string','help'=>'填写一百以内的数字，数字越大表示等级越高！不同等级的等级权重不得相等！'),
			array('name'=>'shoplevel_id','title'=>'绑定商城会员等级','type'=>'select','options'=>'shoplevels'),
			array('name'=>'isyidong','title'=>'是否开启团长转移','type'=>'switch','help'=>'开启后，扫本等级团长二维码后，不过是否已经有上级，都会变更上级'),
			array('name'=>'isyaolevel','title'=>'分享邀请等级','type'=>'radio','options'=>'yidongtype'),
			array('name'=>'set_group','title'=>'分享邀请指定会员等级','type'=>'select','options'=>'groups','help'=>'客户扫了本等级的二维码后，会直接绑定为选择的会员等级（商城会员等级）'),
			array('name'=>'set_feelevelid','title'=>'分享邀请指定分销等级','type'=>'select','options'=>'levels','help'=>'客户扫了本等级的二维码后，会直接绑定为选择的分销等级'),
			array('name'=>'feetype','title'=>'下单奖励类型','type'=>'radio','options'=>'feetype','help'=>'填写后，达到本等级后，奖励余额！'),
			array('name'=>'creditfee','title'=>'等级奖励额','type'=>'unitstring','unit'=>'元','help'=>'填写后，达到本等级后，奖励余额！'),
			array('name'=>'t_level_id','title'=>'到期后降为等级','type'=>'string','type'=>'select','options'=>'levels','help'=>'有效期到了后，会自动降级到指定分销等级！'),
			array('name'=>'commissions.buy_creditfrom','title'=>'下单奖励来源','type'=>'radio','options'=>'buy_creditfrom'),
			array('name'=>'commissions.buy_creditfee','title'=>'下单奖励%','type'=>'unitstring','unit'=>'%','help'=>'奖励积分类型由上面设置的决定！'),
			array('name'=>'creditfee','title'=>'等级奖励余额','type'=>'unitstring','unit'=>'元','help'=>'填写后，达到本等级后，奖励余额！'),
			array('name'=>'ismonth','title'=>'支持月度消费分红','type'=>'switch','help'=>'开启后，该等级会员才享受月度消费分红'),
		))),
	);
	//升级条件表单
	$level_updesc=$this->setting['is_level_up_one']?'满足其中一个就升级':'全满足才升级';
	$condition_edit=array(
		'title'=>'升级条件（'.$level_updesc.'）',
		'list'=>array(),
	);
	$level_up_types=$this->level_up_types;
	$current_level_up_types=$this->model()->current_level_up_types();
	foreach($current_level_up_types as $type){
		if($type=='level_num'){
			$condition_edit['list'][]=array('type'=>'select','name'=>'conditions.level_id','title'=>'选择指定等级','options'=>'level');
		}
		if($type=='buy_goods'){
			$condition_edit['list'][]=array('type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！');
		}elseif($type=='downbuy_goods'){
			$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
			$condition_edit['list'][]=array('type'=>'choose_many','title'=>'购买指定商品升级','name'=>'goods_ids','action'=>'jy_weishop/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>'（多个商品时，任意其中一个）注意：注意触发升级时机，需核销的商品，只有核销后才会完成！虚拟商品，发货后自动会完成！');
		}else{
			$condition_edit['list'][]=array('type'=>'string','name'=>'conditions.'.$type,'title'=>$level_up_types[$type]);
		}
	}
	$htmls['edits'][]=$condition_edit;
	$commission_edit=array(
		'title'=>'佣金比例',
		'list'=>array(),
	);
	if($setting['get_buyordernum']==1){
		$bilititle='级佣金额度（元/笔）';
		$unit='元';
	}else{
		$bilititle='级佣金比例（%）';
		$unit='%';
	}
	for($i=1;$i<=$setting['level'];$i++){
		$commission_edit['list'][]=array('type'=>'unitstring','name'=>'commissions.commission'.$i,'unit'=>$unit,'title'=>$i.''.$bilititle);
	}
	$commission_edit['list'][]=array('type'=>'string','name'=>'commissions.xuni_rate','title'=>'直推获得'.$this->xuniname,'help'=>'填写数字表示实际额度，填写百分数表示比例。例如填写1表示发放1个'.$this->xuniname.'，填写1%表示发放商品价格1%的'.$this->xuniname);
	if($setting['open_otherfee']){
		$otherfee_types=str_array($this->setting['otherfee_types']);
		foreach($otherfee_types as $ov){
			if($ov){
				$ovtitle=$this->model('model')->otherfee_types()[$ov];
				for($i=1;$i<=$setting['level'];$i++){
					$commission_edit['list'][]=array('type'=>'unitstring','name'=>'commissions.other'.$ov.$i,'unit'=>'%','title'=>$i.'级'.$ovtitle);
				}
			}
		}
	}
	$htmls['edits'][]=$commission_edit;
	if($setting['area_open']==1){
		$area_edit=array(
			'title'=>'社区代理',
			'list'=>array(
				array('type'=>'switch','name'=>'area_open','title'=>'是否开启社区代理'),
				array('name'=>'area_rate','title'=>'社区代理佣金','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金'),
				array('name'=>'area_xunirate','title'=>'社区代理'.$this->xuniname,'type'=>'string','help'=>'填写数字表示固定'.$this->xuniname.'，填写%，表示按百分比计算'.$this->xuniname),
			),
		);
		$htmls['edits'][]=$area_edit;
	}
	if($setting['jicha_open']==1){
		$ji_edit=array(
			'title'=>'极差分红',
			'list'=>array(
				array('name'=>'commissions.ji_rate','title'=>'极差分红','type'=>'unitstring','unit'=>'%','help'=>'极差分红规则：上级分红=（上级分红-下级分红-下级分红-下级-下级......），无限下级。平级时不能获得分红！'),
				array('name'=>'commissions.ping_rate','title'=>'平级分红','type'=>'unitstring','unit'=>'%','help'=>'平级分红规则：相对极差分红而言：上下平级，产生的分红'),
			),
		);
		$htmls['edits'][]=$ji_edit;
	}
	if($setting['jicha_chao']==1){
		$ji_edit=array(
			'title'=>'超级奖',
			'list'=>array(
				array('name'=>'commissions.ji_chao','title'=>'超级奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金;')
			),
		);
		$htmls['edits'][]=$ji_edit;
	}
	$return['groups']=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels'][0]=array(
		'id'=>'0',
		'title'=>$page_setting['demolevel'],
		'level'=>0,
	);
	$return['feetype']=$this->credit_type;
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['level']=$this->pdo_getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'level'=>intval($postdata['level']),
		'conditions'=>$postdata['conditions']?ijson_encode($postdata['conditions']):'',
		'commissions'=>$postdata['commissions']?ijson_encode($postdata['commissions']):'',
		'isyidong'=>intval($postdata['isyidong']),
		'isyaolevel'=>intval($postdata['isyaolevel']),
		'set_group'=>intval($postdata['set_group']),
		't_level_id'=>intval($postdata['t_level_id']),
		'creditfee'=>intval($postdata['creditfee']),
		'feetype'=>intval($postdata['feetype']),
		'area_open'=>intval($postdata['area_open']),
		'area_rate'=>$postdata['area_rate'],
		'area_xunirate'=>$postdata['area_xunirate'],
		'goods_ids'=>$postdata['goods_ids']?ijson_encode($postdata['goods_ids']):'',
		'shoplevel_id'=>intval($postdata['shoplevel_id']),
		'set_feelevelid'=>intval($postdata['set_feelevelid']),
		'ismonth'=>intval($postdata['ismonth']),
	);
	if($data['level']<=0 || $data['level']>100){
		exi('请填写正确的等级权重值，1-100','error');
	}
	if(!$data['title']){
		exi('请填写标题','error');
	}
	
	$id=intval($postdata['id']);
	if($id){
		$this->update('level',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('level',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set_default'){
	//设置默认佣金比例
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->plugin_setting('default_level',$post);
		exi('默认等级佣金设置成功！','','list');
	}
	$item=$this->plugin_setting('default_level');
	if(!$item){
		$item=array('commission1'=>'0.00');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置默认佣金',
		'edit'=>array(
			array('name'=>'feetype','title'=>'下单奖励类型','type'=>'radio','options'=>'feetype'),
			array('name'=>'buy_creditfrom','title'=>'下单奖励来源','type'=>'radio','options'=>'buy_creditfrom'),
			array('name'=>'buy_creditfee','title'=>'下单奖励%','type'=>'unitstring','unit'=>'%','help'=>'奖励积分类型由上面设置的决定！'),
		),
	);
	if($setting['get_buyordernum']==1){
		$bilititle='级佣金额度（元/笔）';
		$unit='元';
	}else{
		$bilititle='级佣金比例（%）';
		$unit='%';
	}
	for($i=1;$i<=$setting['level'];$i++){
		$htmls['edit'][]=array('type'=>'unitstring','name'=>'commission'.$i,'unit'=>$unit,'title'=>$i.$bilititle);
	}
	if($setting['open_otherfee']){
		$otherfee_types=str_array($this->setting['otherfee_types']);
		foreach($otherfee_types as $ov){
			if($ov){
				$ovtitle=$this->model('model')->otherfee_types()[$ov];
				for($i=1;$i<=$setting['level'];$i++){
					$htmls['edit'][]=array('type'=>'unitstring','name'=>'commissions.other'.$ov.$i,'unit'=>'%','title'=>$i.'级'.$ovtitle);
				}
			}
		}
	}
	if($setting['jicha_open']==1){
		$htmls['edit'][]=array('name'=>'ji_rate','title'=>'极差分红','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金;极差分红规则：上级分红=（上级分红-下级分红-下级分红-下级-下级......），无限下级。平级时不能获得分红！');
		$htmls['edit'][]=array('name'=>'ping_rate','title'=>'平级分红','type'=>'unitstring','unit'=>'%','help'=>'平级分红规则：相对极差分红而言：上下平级，产生的分红');
	}
	if($setting['jicha_chao']==1){
		$htmls['edit'][]=array('name'=>'ji_chao','title'=>'超级奖','type'=>'string','help'=>'填写数字表示固定佣金，填写%，表示按百分比计算佣金;');
	}
	$return['feetype']=$this->credit_type;
	$return['buy_creditfrom']=$this->buy_creditfrom;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$ismember=pdo_count('jy_commission_member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$id));
		if($ismember){
			exi('该等级下已经有会员，不能删除，否则会影响会员分佣情况！','error');
		}
		pdo_delete('jy_commission_level',array('id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$ismember=pdo_count('jy_commission_member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$id));
				if($ismember){
					exi('该等级下已经有会员，不能删除，否则会影响会员分佣情况！','error');
				}
				pdo_delete('jy_commission_level',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>